import { BlockInputEvents, Canvas, director, Layers, Node, UITransform, v3, view } from 'cc';

/**
 * Cocos Creator BlockInputEvents组件相关拓展
 */
export default class BlockMask {

    /**
     * 获取当前场景下的 EventBlockMask 节点
     */
    static get maskLayer(): Node {
        return director.getScene().getChildByName('EventBlockMask');
    }
    /**
     * 判断当前场景是否存在 EventBlockMask 节点
     */
    static get isLayerValid(): boolean {
        return !!BlockMask.maskLayer && BlockMask.maskLayer.isValid;
    }

    /**
     * 添加全屏禁止触摸节点
     */
    static addEventBlock(): Node {
        if (BlockMask.isLayerValid)
            return BlockMask.maskLayer;
        const viewSize = view.getVisibleSize();
        const mask = new Node();
        mask.name = 'EventBlockMask';
        mask.layer = Layers.Enum.UI_2D;
        mask.setPosition(v3(viewSize.width / 2, viewSize.height / 2, 0));
        mask.addComponent(UITransform).setContentSize(viewSize);
        mask.addComponent(BlockInputEvents);
        const canvas = mask.addComponent(Canvas);
        canvas.alignCanvasWithScreen = true;
        director.getScene().addChild(mask);
        return mask;
    }

    /**
     * 移除全屏禁止触摸节点
     */
    static delEventBlock(): void {
        if (BlockMask.isLayerValid)
            BlockMask.maskLayer.destroy();
    }

}

